﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ExamDemo.Data;
using ExamDemo.Models;
using ExamDemo.ViewModel;

namespace ExamDemo.Controllers
{
    public class SayController : Controller
    {
        private SayDb db = new SayDb();

        // GET: Say
        public ActionResult Index(int id)
        {

            var cuuentUser = db.Users.Where(x => x.Id == id).SingleOrDefault();

            //获取所有用户
            var userList = db.Users.ToList();
            //获取所有说说
            var msgList = db.Messages.ToList();
            //获取所有评论
            var comList = db.Comments.ToList();

            //定义一个所有说说的模型
            var msgViewsModelList = new List<MessagesViewsModel>();

          

                //遍历所有说说
                foreach (var msg in msgList)
                {

                    //定义一个评论的模型,用于存放当前说说的评论
                    var comViewsModelList = new List<CommentsViewsModel>();

                    // 根据当前说说id，查找所有的评论
                    var cuuentMsgComment = comList.Where(x => x.MsgId == msg.Id).ToList();

                    //遍历所有评论
                    foreach (var com in cuuentMsgComment)
                    {

                        comViewsModelList.Add(new CommentsViewsModel
                        {
                            //根据用户id等于当前评论的用户id,获取当前评论说说的用户名
                            FromUserName = userList.SingleOrDefault(x => x.Id == com.FromUserId).Username,
                            //根据说说表的id==评论表的说说id,获取当前评论的说说id，再根据说说id获取当前说说的用户id,再根据用户id==用户表的id获取用户名
                            ToUserName = userList.SingleOrDefault(x => x.Id == msgList.SingleOrDefault(y => y.Id == com.MsgId).FromUserId).Username,
                            Comment = com.Content
                        });
                    }

                    //msg 为当前说说
                    msgViewsModelList.Add(new MessagesViewsModel
                    {
                        Id = msg.Id,
                        FromUserId = msg.FromUserId,
                        Content = msg.Content,
                        //评论模块
                        Comments = comViewsModelList

                    });
                }
                var data = new SayViewsModel
                {
                    Username = cuuentUser.Username,
                    Messages = msgViewsModelList
                };

                return View(data);
            }
        
        // GET: Say/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // GET: Say/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Say/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Username,Password,CreatedAt,UpdatedAt,Version,Remarks")] Users users)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(users);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(users);
        }

        // GET: Say/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // POST: Say/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性。有关
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Username,Password,CreatedAt,UpdatedAt,Version,Remarks")] Users users)
        {
            if (ModelState.IsValid)
            {
                db.Entry(users).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(users);
        }

        // GET: Say/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Users users = db.Users.Find(id);
            if (users == null)
            {
                return HttpNotFound();
            }
            return View(users);
        }

        // POST: Say/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Users users = db.Users.Find(id);
            db.Users.Remove(users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
